<?php
namespace app\admin\controller\operator;

use app\admin\controller\Controller;
use think\Db;

/**
 * 数据分析
 * Class Data
 * @package app\admin\controller\statistics
 */
class Agent extends Controller
{

    /**
     * 品牌商列表
     * @return mixed
     * @throws \think\Exception
     */
    public function agent(){

        $param = $this->request->param();

        //品牌商列表
        $where = [];
        if (isset($param['search']) && !empty($param['search'])) {
            $where["company_name"]= ["like",'%'.trim($param['search']).'%'];
        }
        //平台总分润
        $plant = db('divide_system')->where(['status'=>1])->sum('amount');
        //品牌商列表
        $list = db('agent')
            ->where(['type'=>0,'active'=>1,'is_recycle'=>0,'is_delete'=>0])
            ->where($where)
            ->field('agent_id,type,company_name,real_name,company_tel,recommend_id,agent_check_status,admin_check_status,create_time')
            ->order('create_time desc')
            ->paginate(15, false, [
                'query' => request()->request()
            ])->each(function($item,$key){

                //计算大代理个数，即城市运营商
                $item['maxganet'] = Db::name('agent')
                    ->where(['type'=>1,'active'=>1,'recommend_id'=>$item['agent_id'],'is_recycle'=>0,'is_delete'=>0])
                    ->count();

                //计算小代理个数，即城市合伙人
                $item['minganet'] = Db::name('agent')
                    ->where(['type'=>2,'active'=>1,'recommend_id'=>$item['agent_id'],'is_recycle'=>0,'is_delete'=>0])
                    ->count();

                //计算品牌商直接推荐的商户的个数
                $item['agentstore'] = Db::name('wxapp')
                    ->where(['active'=>1,'agent_id'=>$item['agent_id']])
                    ->count();

                //计算品牌商直接推荐的员推荐的商户的个数
                $agent_clerk_list =  Db::name('agent')
                    ->where(['type'=>3,'active'=>1,'recommend_id'=>$item['agent_id'],'is_recycle'=>0,'is_delete'=>0])
                    ->select();

                $clerk_num = 0;
                foreach ($agent_clerk_list as $kk=>&$vv){
                    $clerk_num+=  Db::name('wxapp')
                        ->where(['active'=>1,'agent_id'=>$vv['agent_id']])
                        ->count();
                }

                $item['clerk_num'] =   $clerk_num;

                //计算品牌商分润
                $item['agentmoney'] = Db::name('divide_service')
                    ->where(['agent_id'=>$item['agent_id'],'status'=>1])
                    ->sum('amount');

                $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);

                return $item;
            });

        return $this->fetch('agent', compact( 'list','plant'));
    }



    /**
     * 运营商
     * @return mixed
     * @throws \think\Exception
     */
    public function one(){

        $param = $this->request->param();

        $where = ['recommend_id'=>$param['agent_id']];
        if (isset($param['search']) && !empty($param['search'])) {
            $where["company_name|real_name"]= ["like",'%'.trim($param['search']).'%'];
        }
        //运营商列表
        $list = db('agent')
            ->where(['type'=>1,'active'=>1,'is_recycle'=>0,'is_delete'=>0])
            ->where($where)
            ->field('agent_id,type,company_name,real_name,company_tel,recommend_id,agent_check_status,admin_check_status,create_time')
            ->order('create_time desc')
            ->paginate(15, false, [
                'query' => request()->request()
            ])->each(function($item,$key){


                //计算运营商直接推荐的商户的个数
                $item['agentstore'] = Db::name('wxapp')
                    ->where(['active'=>1,'agent_id'=>$item['agent_id']])
                    ->count();
                //计算小代理个数，即城市合伙人
                $item['minganet'] = Db::name('agent')
                    ->where(['type'=>2,'active'=>1,'recommend_id'=>$item['agent_id'],'is_recycle'=>0,'is_delete'=>0])
                    ->count();

                //计算运营商分润
                $item['agentmoney'] = Db::name('divide_service')
                    ->where(['agent_id'=>$item['agent_id'],'status'=>1])
                    ->sum('amount');

                //计算运营商直接推荐的员推荐的商户的个数
                $agent_clerk_list =  Db::name('agent')
                    ->where(['type'=>3,'active'=>1,'recommend_id'=>$item['agent_id'],'is_recycle'=>0,'is_delete'=>0])
                    ->select();

                $clerk_num = 0;
                foreach ($agent_clerk_list as $kk=>&$vv){
                    $clerk_num+=  Db::name('wxapp')
                        ->where(['active'=>1,'agent_id'=>$vv['agent_id']])
                        ->count();
                }

                $item['clerk_num'] =   $clerk_num;

                $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);

                return $item;
            });

        return $this->fetch('one', compact( 'list'));
    }





    /**
     * 合伙人
     * @return mixed
     * @throws \think\Exception
     */
    public function tow(){


        $param = $this->request->param();

        $where = ['recommend_id'=>$param['agent_id']];
        if (isset($param['search']) && !empty($param['search'])) {
            $where["company_name|real_name"]= ["like",'%'.trim($param['search']).'%'];
        }
        //合伙人列表
        $list = db('agent')
            ->where(['type'=>2,'active'=>1,'is_recycle'=>0,'is_delete'=>0])
            ->where($where)
            ->field('agent_id,type,company_name,real_name,company_tel,recommend_id,agent_check_status,admin_check_status,create_time')
            ->order('create_time desc')
            ->paginate(15, false, [
                'query' => request()->request()
            ])->each(function($item,$key){


                //计算品牌商推荐的商户的个数
                $item['agentstore'] = Db::name('wxapp')
                    ->where(['active'=>1,'agent_id'=>$item['agent_id']])
                    ->count();

                //计算品牌商分润
                $item['agentmoney'] = Db::name('divide_service')
                    ->where(['agent_id'=>$item['agent_id'],'status'=>1])
                    ->sum('amount');

                $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);

                //计算运营商直接推荐的员推荐的商户的个数
                $agent_clerk_list =  Db::name('agent')
                    ->where(['type'=>3,'active'=>1,'recommend_id'=>$item['agent_id'],'is_recycle'=>0,'is_delete'=>0])
                    ->select();

                $clerk_num = 0;
                foreach ($agent_clerk_list as $kk=>&$vv){
                    $clerk_num+=  Db::name('wxapp')
                        ->where(['active'=>1,'agent_id'=>$vv['agent_id']])
                        ->count();
                }

                $item['clerk_num'] =   $clerk_num;

                return $item;
            });

        return $this->fetch('tow', compact( 'list'));
    }


}